﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc; // needed for lambda includes
using TheCup.ApiControllers;

namespace TheCup.Controllers
{
    public class RoundController : Controller
    {
        ApiControllers.RoundController rc = new ApiControllers.RoundController();
        ApiControllers.CourseController coursecontroller = new ApiControllers.CourseController();
        ApiControllers.EventController ec = new ApiControllers.EventController();
        ApiControllers.PlayerController pc = new ApiControllers.PlayerController();
        CommentController cc = new CommentController();
        ScoreController scc = new ScoreController();

        public ActionResult Index()
        {
            if (!string.IsNullOrEmpty(Request.Params["show"]) && Request.Params["show"] == "all")
                return View(rc.Get());
            else
                return View(rc.Get().Take(50));
        }

        [Authorize]
        public ActionResult MyRounds()
        {
            Player p = pc.GetLoggedInPlayer();
            List<Round> e = new List<Round>();
            if (p != null)
            {
                e = rc.GetForPlayer(p.Id).ToList();
            }
            return View(e);
        }

        public ActionResult Details(int id)
        {
            Round r = rc.Get(id);
            // TODO: move this logic
            r.CalculatePositionsByNettoscore();
            r.PrepareScorecardsForRankingLists();

            r.Comments = cc.Get(r.uuid, r.Id, CommentEntity.Round);

            return View(r);
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            Round e = rc.Get(id);
            ViewBag.Events = ec.GetRecentEvents();
            ViewBag.Courses = coursecontroller.Get();
            ViewBag.Tees = new ApiControllers.TeeController().GetForCourse(e.CourseId);
            ViewBag.Players = pc.Get();
            return View(e);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Edit(string btnSubmit, Round e)
        {
            if (ModelState.IsValid)
            {
                switch (btnSubmit)
                {
                    case "save":
                        rc.Put(e.Id, e);
                        break;
                        
                    case "delete":
                        rc.Delete(e.Id);
                        break;
                                        
                    default:
                        break;
                }
            }
            return RedirectToAction("MyRounds", "Round");
        }

        [Authorize]
        public ActionResult Create()
        {
            Round e = new Round();
            e.DateEntry = DateTime.UtcNow.Date;
            ViewBag.Events = ec.GetRecentEvents();
            ViewBag.Courses = coursecontroller.Get();
            return View(e);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create(Round e)
        {
            if (ModelState.IsValid)
            {
                rc.Post(e);
                return RedirectToAction("Edit", new { id = e.Id });
            }
            else
            {
                ViewBag.Events = ec.Get();
                ViewBag.Courses = coursecontroller.Get();
                return View(e);
            }
        }
    }
}
